<script setup>
import {ref, reactive, computed, onBeforeMount} from "vue"
import {useStore} from "vuex"
import * as api from "./api.js"
import businessModules from "@business/modules.js"

const {$fixedColumn, $widthColumn} = window

const store = useStore()
const userInfo = computed(() => store.getters.userInfo)

const state = reactive({
    list: [],
    isLoading: false,
})

/**
 * 同步菜单
 */
const syncMenu = () => {
    businessModules().then(modules => {
        console.log(modules);
        console.log(JSON.stringify(modules));
        api.AddMenuList(modules).then((res) => {
            if (res.success) {
                $message.success("操作成功");
                loadSystemMenuTree()
            }
        })
    })
}

const loadSystemMenuTree = () => {
    state.isLoading = true
    api.GetMenuTreeList().then(res => {
        state.isLoading = false
        state.list = res.data || []
    })
}

// 复制
function setCopy(text) {
    window.$copyText(text)
}


onBeforeMount(() => {
    if (userInfo.value.isAdmin) {
        loadSystemMenuTree()
    }
})

</script>

<template>
    <div v-if="userInfo.isAdmin" class="initMenu">
        <div class="container">
            <div class="table-buttons">
                <el-button type="primary" @click="syncMenu()">
                    <b-icon name="bi-arrow-repeat" size="20" />
                    同步菜单
                </el-button>
            </div>
            <vxe-table ref="VXETable" :column-config="{ resizable: false}" :data="state.list" :loading="state.isLoading" :radio-config="{trigger: 'row', highlight: true}" :row-config="{isCurrent: true, isHover: true, keyField: 'id'}" :tree-config="{childrenField: 'children', reserve: true}" border round show-overflow size="small" @current-change="() => {}" @radio-change="() => {}">
                <template #empty>
                    <el-empty :image-size="100" />
                </template>

                <vxe-column fixed="left" title="#" type="seq" width="80" />
                <vxe-column :fixed="$fixedColumn('left')" field="name" min-width="200" title="名称" tree-node>
                    <template #default="{row}">
                        <b-icon v-if="row.icon" :name="row.icon" size="14" style="margin-right: 4px;color: #409EFF;" />
                        <div :class="{menuVisible: row.visible}" class="menu-name">
                            <!--                            <div class="menu-status"></div>-->
                            <span>{{ row.name }}</span>
                        </div>
                    </template>
                </vxe-column>
                <vxe-column align="center" field="type" title="类型" width="100">
                    <template #default="{row}">
                        <el-tag v-if="row.type===0" effect="dark" type="success">目录</el-tag>
                        <el-tag v-if="row.type===1" effect="dark" type="warning">页面</el-tag>
                        <el-tag v-if="row.type===2" effect="dark" type="danger">功能</el-tag>
                    </template>
                </vxe-column>
                <vxe-column field="code" title="编号" width="250">
                    <template #default="{row}">
                        <b-icon name="bi-clipboard-check" size="14" style="margin-right: 4px;color: #409EFF;cursor: pointer;" @click.stop="setCopy(row.code)" />
                        <span>{{ row.code }}</span>
                    </template>
                </vxe-column>
                <vxe-column align="center" field="openType" title="打开方式" width="100">
                    <template #default="{row}">
                        <template v-if="row.type===1">
                            <el-tag v-if="row.openType==='1'" effect="plain" type="success">组件</el-tag>
                            <el-tag v-if="row.openType==='2'" effect="plain" type="warning">内链</el-tag>
                            <el-tag v-if="row.openType==='3'" effect="plain" type="danger">外链</el-tag>
                        </template>
                    </template>
                </vxe-column>
                <vxe-column align="left" field="path" title="地址" width="280">
                    <template #default="{row}">
                        <template v-if="row.type===1">
                            <span v-if="row.openType===1">
                                <b-icon name="bi-clipboard-check" size="14" style="margin-right: 4px;color: #409EFF;cursor: pointer;" @click.stop="setCopy(row.path)" />
                                <span>{{ row.path }}</span>
                            </span>
                            <span v-if="row.openType===1">
                                <b-icon name="bi-clipboard-check" size="14" style="margin-right: 4px;color: #409EFF;cursor: pointer;" @click.stop="setCopy(row.link)" />
                                <span>{{ row.link }}</span>
                            </span>
                            <span v-if="row.openType===1">
                                <b-icon name="bi-clipboard-check" size="14" style="margin-right: 4px;color: #409EFF;cursor: pointer;" @click.stop="setCopy(row.link)" />
                                <span>{{ row.link }}</span>
                            </span>
                        </template>
                    </template>
                </vxe-column>
                <!--                <vxe-column align="center" field="isLockDataPermission" title="锁定数据权限" width="160">-->
                <!--                    <template #default="{row}">-->
                <!--                        <el-tag v-if="row.isLockDataPermission===1 && row.dataPermission===0">全部</el-tag>-->
                <!--                        <el-tag v-if="row.isLockDataPermission===1 && row.dataPermission===1" type="warning">组织(包含子组织)</el-tag>-->
                <!--                        <el-tag v-if="row.isLockDataPermission===1 && row.dataPermission===2" type="danger">组织(当前)</el-tag>-->
                <!--                        <el-tag v-if="row.isLockDataPermission===1 && row.dataPermission===3" type="danger">个人</el-tag>-->
                <!--                    </template>-->
                <!--                </vxe-column>-->
                <vxe-column field="permission" min-width="200" title="权限" />
                <vxe-column align="center" field="sort" title="排序" width="80" />
                <vxe-column :resizable="false" :width="$widthColumn(150)" field="action" fixed="right" title="操作">
                    <template #default="{row}">
                        <!--                        <table-action :actions="[-->
                        <!--                            {title: '修改', permission: permission.edit, type: 'primary', ok: editData},-->
                        <!--                            {title: '删除', permission: permission.delete, type: 'danger', ok: deleteData, confirm: '您确定要删除当前数据么?'},-->
                        <!--                            {title: '添加下级菜单', show: row.type < 2, permission: permission.add, type: 'primary', ok: addSubData, inMore: true},-->
                        <!--                        ]" :data="row" />-->
                        <!--                        <table-action v-if="row.type === 0" :actions="[-->
                        <!--                            {title: '修改', permission: permission.edit, type: 'primary', ok: () => onDirectory('edit', row)},-->
                        <!--                            {title: '删除', permission: permission.delete, type: 'danger', ok: deleteData, confirm: '您确定要删除当前数据么?'},-->
                        <!--                            {title: '添加目录', show: !row.pid, permission: permission.add, type: 'primary', ok: () => onDirectory('addSub', row), inMore: true},-->
                        <!--                            {title: '添加页面', permission: permission.add, type: 'primary', ok: () => onPage('add', row), inMore: true},-->
                        <!--                        ]" :data="row" />-->
                        <!--                        <table-action v-if="row.type === 1" :actions="[-->
                        <!--                            {title: '修改', permission: permission.edit, type: 'primary', ok: () => onPage('edit', row)},-->
                        <!--                            {title: '删除', permission: permission.delete, type: 'danger', ok: deleteData, confirm: '您确定要删除当前数据么?'},-->
                        <!--                            {title: '添加菜单权限', show: row.openType === '1', permission: permission.add, type: 'primary', ok: () => onButtonPermission('add', row), inMore: true},-->
                        <!--                        ]" :data="row" />-->
                        <!--                        <table-action v-if="row.type === 2" :actions="[-->
                        <!--                            {title: '修改', permission: permission.edit, type: 'primary', ok: () => onButtonPermission('edit', row)},-->
                        <!--                            {title: '删除', permission: permission.delete, type: 'danger', ok: deleteData, confirm: '您确定要删除当前数据么?'},-->
                        <!--                        ]" :data="row" />-->
                    </template>
                </vxe-column>
            </vxe-table>
        </div>

    </div>
    <el-empty v-else />
</template>

<style lang="scss" scoped>
.initMenu {
    margin: 10px;
    width: calc(100% - 20px);
    box-sizing: border-box;

    .container {
        padding: 10px;
        background: #fff;
        margin-top: 10px;
        box-shadow: 0 0 10px -2px rgba(0, 0, 0, 0.1);

        .menu-name {
            display: inline-flex;
            flex-direction: row;
            justify-content: flex-start;
            align-items: center;
            color: var(--el-color-danger);
            font-size: 14px;

            .menu-status {
                display: inline-block;
                width: 6px;
                height: 6px;
                border-radius: 10px;
                border: 1px solid var(--el-color-danger);
                margin-right: 4px;
            }
        }

        .menuVisible {
            color: var(--el-color-primary);

            .menu-status {
                border: 1px solid var(--el-color-primary);
                background: var(--el-color-primary);
                animation: visible 0.8s ease infinite;
            }
        }
    }
}

@keyframes visible {
    0% {
        box-shadow: 0 0 4px 0 rgba(0, 0, 0, 0)
    }
    50% {
        box-shadow: 0 0 4px 0 var(--el-color-primary)
    }
    100% {
        box-shadow: 0 0 4px 0 rgba(0, 0, 0, 0)
    }
}
</style>